﻿/*
 * net_csharp.h
 *
 */
#pragma once


 ////////////////////////////////////////////////////////////////
#ifdef GDK_SIMPLE_DLL_BUILD
#	define GDK_SIMPLE_EXPORT	__DECLSPEC__(__DLLEXPORT__)
#elif !defined(GDK_SIMPLE_LIB_BUILD)
#	define GDK_SIMPLE_EXPORT	__DECLSPEC__(__DLLIMPORT__)
#else
#	define GDK_SIMPLE_EXPORT
#endif


////////////////////////////////////////////////////////////////
#include <string>
#include <gdk/net/NetModule.h>
#include <SimpleNetwork/NetMessage.h>

#if !(defined(__IOS__) || defined(__ANDROID__) || defined(_WIN32))
#   define DISABLE_PATCH 1
#endif

namespace simple
{

	////////////////////////////////////////////////////////////////
	// 定义回调函数
	typedef void(*SimpleCallback_NetModule_Start)(const char *protocol, int port);
	typedef void(*SimpleCallback_NetModule_Shutdown)();
	typedef void(*SimpleCallback_NetModule_Accept)();
	typedef void(*SimpleCallback_ConnectOk)(class SimpleSession* conn);
	typedef void(*SimpleCallback_ConnectFail)(class SimpleSession* conn);
	typedef void(*SimpleCallback_Blocking)(class SimpleSession* conn);
	typedef void(*SimpleCallback_Restored)(class SimpleSession* conn);
	typedef void(*SimpleCallback_Closed)(class SimpleSession* conn);
	typedef void(*SimpleCallback_Error)(class SimpleSession* conn);
	typedef void(*SimpleCallback_Abort)(class SimpleSession* conn);
	typedef void(*SimpleCallback_Received)(class SimpleSession* conn, const char* data, int size);

	typedef void(*GDK_Log_Callback_Debug)(const char* text);
	typedef void(*GDK_Log_Callback_Warning)(const char* text);
	typedef void(*GDK_Log_Callback_Error)(const char* text);
	typedef void(*GDK_Log_Callback_Message)(const char* text);

#ifndef DISABLE_PATCH
	typedef void(*Patch_Callback_Start)(const char *url, const char *path);
	typedef void(*Patch_Callback_Schedule)(int64_t totalSize, int64_t curPos, int64_t step);
	typedef void(*Patch_Callback_Done)(int64_t size);
	typedef void(*Patch_Callback_Fail)(void);
#endif


	////////////////////////////////////////////////////////////
	class GDK_SIMPLE_EXPORT SimpleSession : public gdk::NetSession
	{
		DECLARE_CLASS_HANDLE(SimpleSession)
		DECLARE_CLASS_INSTANCE1(SimpleSession, class gdk::NetModule *)

		SimpleCallback_ConnectOk     _SimpleCallback_ConnectOk;
		SimpleCallback_ConnectFail   _SimpleCallback_ConnectFail;
		SimpleCallback_Blocking      _SimpleCallback_Blocking;
		SimpleCallback_Restored      _SimpleCallback_Restored;
		SimpleCallback_Closed        _SimpleCallback_Closed;
		SimpleCallback_Error         _SimpleCallback_Error;
		SimpleCallback_Abort         _SimpleCallback_Abort;
		SimpleCallback_Received      _SimpleCallback_Received;

		int32_t                      _sendBytes; // 发送总字节
		int32_t                      _recvBytes; // 接收总字节
		int32_t                      _writeCompress; // 发送压缩总字节
		int32_t                      _readCompress;  // 接收压缩总字节

		bool                         _compressflag; // 客户端是否接收压缩数据包标志
		gdk::Buffer                  _writebuffer;
		gdk::Buffer                  _readbuffer;

	protected:
		SimpleSession(class gdk::NetModule *netModule);

	public:
		//----------------------------------------------------------
		virtual bool accept(void);
		virtual bool connect(const gdk::NetAddress &addr, const gdk::Uuid &socketId = UUID_NULL);
		virtual bool send(gdk::Message *msg);
		virtual bool send(const char *data, int size, int flag); // flag = 0 自动, 1 不压缩, 2, 压缩
		//----------------------------------------------------------
		virtual void onConnectOk(const gdk::Evt_NetConnectOk *evt);
		virtual void onConnectFail(const gdk::Evt_NetConnectFail *evt);
		virtual void onBlocking(const gdk::Evt_NetBlocking *evt);
		virtual void onRestored(const gdk::Evt_NetRestored *evt);
		virtual void onClosed(const gdk::Evt_NetClosed *evt);
		virtual void onError(const gdk::Evt_NetError *evt);
		virtual void onAbort(const gdk::Evt_NetAbort *evt);
		//---------------------------------------------------------
		virtual void onSimplePackage(SimplePackage *msg);
		virtual void onSimplePackage2(SimplePackage2 *msg);
		//---------------------------------------------------------
	public:
		void setCallback_ConnectOk(SimpleCallback_ConnectOk callback);
		void setCallback_ConnectFail(SimpleCallback_ConnectFail callback);
		void setCallback_Blocking(SimpleCallback_Blocking callback);
		void setCallback_Restored(SimpleCallback_Restored callback);
		void setCallback_Closed(SimpleCallback_Closed callback);
		void setCallback_Error(SimpleCallback_Error callback);
		void setCallback_Abort(SimpleCallback_Abort callback);
		void setCallback_Received(SimpleCallback_Received callback);
		//---------------------------------------------------------
		const std::string& getUuid() const { return _uuid; };
		//---------------------------------------------------------
		int32_t getSendBytes(void) const { return _sendBytes; }
		int32_t getRecvBytes(void) const { return _recvBytes; }
		int32_t getWriteCompress(void) const { return _writeCompress; }
		int32_t getReadCompress(void) const { return _readCompress; }
		void    resetStatistics(void) { _sendBytes = _recvBytes = _writeCompress = _readCompress = 0; }
		//---------------------------------------------------------
	private:
		std::string _uuid;
	};
	typedef SimpleSession::Handle SimpleSessionPtr;


	////////////////////////////////////////////////////////////////
	extern "C" {

		GDK_SIMPLE_EXPORT bool SimpleNetwork_createNetModule(const char *protocol, int port = 0, gdk::Semaphore *semaphore = NULL);
		GDK_SIMPLE_EXPORT void SimpleNetwork_destroyNetModule(void);
		GDK_SIMPLE_EXPORT void SimpleNetwork_tickNetModule(uint32_t timeout = 50);
		GDK_SIMPLE_EXPORT SimpleSession* SimpleNetwork_accept(void);
		GDK_SIMPLE_EXPORT SimpleSession* SimpleNetwork_connect(const char *ip, int port);
		GDK_SIMPLE_EXPORT const char *SimpleNetwork_uuid(SimpleSession* conn);
		GDK_SIMPLE_EXPORT bool SimpleNetwork_send(SimpleSession* conn, const char *data, int size, int flag = 0); // flag = 0 自动, 1 不压缩, 2, 压缩
		GDK_SIMPLE_EXPORT void SimpleNetwork_close(SimpleSession* conn);
		GDK_SIMPLE_EXPORT void setSimpleCallback_NetModule_Start(SimpleCallback_NetModule_Start callback);
		GDK_SIMPLE_EXPORT void setSimpleCallback_NetModule_Shutdown(SimpleCallback_NetModule_Shutdown callback);
		GDK_SIMPLE_EXPORT void setSimpleCallback_NetModule_Accept(SimpleCallback_NetModule_Accept callback);
		GDK_SIMPLE_EXPORT void setSimpleCallback_ConnectOk(SimpleSession* conn, SimpleCallback_ConnectOk callback);
		GDK_SIMPLE_EXPORT void setSimpleCallback_ConnectFail(SimpleSession* conn, SimpleCallback_ConnectFail callback);
		GDK_SIMPLE_EXPORT void setSimpleCallback_Blocking(SimpleSession* conn, SimpleCallback_Blocking callback);
		GDK_SIMPLE_EXPORT void setSimpleCallback_Restored(SimpleSession* conn, SimpleCallback_Restored callback);
		GDK_SIMPLE_EXPORT void setSimpleCallback_Closed(SimpleSession* conn, SimpleCallback_Closed callback);
		GDK_SIMPLE_EXPORT void setSimpleCallback_Error(SimpleSession* conn, SimpleCallback_Error callback);
		GDK_SIMPLE_EXPORT void setSimpleCallback_Abort(SimpleSession* conn, SimpleCallback_Abort callback);
		GDK_SIMPLE_EXPORT void setSimpleCallback_Received(SimpleSession* conn, SimpleCallback_Received callback);

		GDK_SIMPLE_EXPORT int32_t setSimpleCallback_getSendBytes(SimpleSession* conn);
		GDK_SIMPLE_EXPORT int32_t setSimpleCallback_getRecvBytes(SimpleSession* conn);
		GDK_SIMPLE_EXPORT int32_t setSimpleCallback_getWriteCompress(SimpleSession* conn);
		GDK_SIMPLE_EXPORT int32_t setSimpleCallback_getReadCompress(SimpleSession* conn);
		GDK_SIMPLE_EXPORT void    setSimpleCallback_resetStatistics(SimpleSession* conn);

		GDK_SIMPLE_EXPORT void GDK_Log_start(void);
		GDK_SIMPLE_EXPORT void GDK_Log_stop(void);
		GDK_SIMPLE_EXPORT void GDK_Log_tick(void);
		GDK_SIMPLE_EXPORT void GDK_Log_setCallback_Debug   (GDK_Log_Callback_Debug callback);
		GDK_SIMPLE_EXPORT void GDK_Log_setCallback_Warning (GDK_Log_Callback_Warning callback);
		GDK_SIMPLE_EXPORT void GDK_Log_setCallback_Error   (GDK_Log_Callback_Error callback);
		GDK_SIMPLE_EXPORT void GDK_Log_setCallback_Message (GDK_Log_Callback_Message callback);

#ifndef DISABLE_PATCH
		GDK_SIMPLE_EXPORT bool Patch_init(const char *url, const char *path);
		GDK_SIMPLE_EXPORT bool Patch_start(void);
		GDK_SIMPLE_EXPORT void Patch_stop(void);
		GDK_SIMPLE_EXPORT void Patch_tick(void);
		GDK_SIMPLE_EXPORT int64_t Patch_getPatchSize(void);
		GDK_SIMPLE_EXPORT void Patch_setCallback_Start    (Patch_Callback_Start callback);
		GDK_SIMPLE_EXPORT void Patch_setCallback_Schedule (Patch_Callback_Schedule callback);
		GDK_SIMPLE_EXPORT void Patch_setCallback_Done     (Patch_Callback_Done callback);
		GDK_SIMPLE_EXPORT void Patch_setCallback_Fail     (Patch_Callback_Fail callback);
#endif

	}


}//namespace simple



